package ch.rheintalconnection.sysventory.businessServices;

import javassist.NotFoundException;

import org.apache.commons.codec.digest.DigestUtils;

import ch.rheintalconnection.sysventory.domain.User;
import ch.rheintalconnection.sysventory.technicalServices.IDataReader;

public class LoginService {

	private IDataReader dataReader;

	public LoginService(IDataReader dataReader) {
		super();
		this.dataReader = dataReader;
		// this.users = dataReader.selectAllUsers();
	}

	public Boolean validate(String username, String password)
	{
		String cryptedUsername = DigestUtils.md5Hex(username);
		String cryptedPassword = DigestUtils.md5Hex(password);
		
		User user = dataReader.selectUser(cryptedUsername, cryptedPassword);
		
		if(user!=null)
		{
			return true;
		}
		return false;
	}
	
	public User getUser(String username, String password) throws Exception
	{
		String cryptedUsername = DigestUtils.md5Hex(username);
		String cryptedPassword = DigestUtils.md5Hex(password);
		
		User user = dataReader.selectUser(cryptedUsername, cryptedPassword);
		
		if(user==null)
		{
			throw new NotFoundException("User: "+username+" not found!");
		}
		return user;
	}
	
}
